import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.io.RandomAccessFile;

//Class containing operations of datatype r4
public class r4 extends datatype implements read,write{

	public void write(String data, int length) throws IOException
	{
		//System.out.println("r4 write called");
		RandomAccessFile fi= new RandomAccessFile(heap.f,"rw");
		fi.seek(heap.f.length());
		float b=Float.parseFloat(data);
		fi.writeFloat(b);
		fi.close();
	}
	public String read(int length) throws IOException
	{
		String str;
		RandomAccessFile fip= new RandomAccessFile(heap.f, "rw");
		fip.seek(heap.readpointer);
		str=""+fip.readFloat();
		heap.readpointer+=4;
		fip.close();
		return str;
	}
	public int compares(String value1,String value2) throws IOException
	{
		int result=0;
		float record1=Float.parseFloat(value1);
		float record2=Float.parseFloat(value2);
		result = record1>record2?1:record1<record2?2:0;
		return result;

	}
	public Object convert(String str)
	{	
		float f = Float.parseFloat(str);
		return f;
	}
	public byte[] returnPrimitive(String str)
	{
		 float f = Float.parseFloat(str);
		 int i=Float.floatToRawIntBits(f);
		 return new byte[] {
					
					(byte)((i >> 24) & 0xff),	
					(byte)((i >> 16) & 0xff),
					(byte)((i >> 8) & 0xff),
					(byte)((i >> 0) & 0xff),
					
			};
	}
	public Object getPrimitive(byte[] data)
	{
		if (data == null || data.length != 4) return 0x0;
		// ---------- simple:
		return Float.intBitsToFloat(toInt(data));
	}

	public static int toInt(byte[] data) {
		if (data == null || data.length != 4) return 0x0;
		// ----------
		return (int)( // NOTE: type cast not necessary for int
		(0xff & data[0]) << 24 |
		(0xff & data[1]) << 16 |
		(0xff & data[2]) << 8 |
		(0xff & data[3]) << 0
		);
		}
}
